package com.chookss.home.knowledge.adaper;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chookss.MyApp;
import com.chookss.R;
import com.chookss.home.document.DocumentBaseDirectorySecondActivity;
import com.chookss.home.entity.KnowledgeEntity;
import com.chookss.home.knowledge.KnowledgeBaseSecondActivity;
import com.chookss.video.VideoNewCatesSecondActivity;
import com.chookss.video.VideoNewCatesSecondActivity2;
import com.chookss.view.flowLayout.FlowAdapter;

import java.util.ArrayList;

public class FlowMoreAdapter extends FlowAdapter<KnowledgeEntity.ChildListBean> {
    private Context context;
    private int type = 0;//0知识库  1视频2资料库
    private String videoType = "";//视频所用字段
    private KnowledgeEntity levelOne;//一级目录数据
    @Override
    public View getView(ViewGroup parent, KnowledgeEntity.ChildListBean item, int position) {
        return LayoutInflater.from(parent.getContext()).inflate(R.layout.item_knowledge_name_list, null);
    }
    public void setData(Context context, KnowledgeEntity levelOne, int type, String videoType){
        this.context = context;
        this.videoType = videoType;
        this.type = type;
        this.levelOne = levelOne;
    }
    @Override
    public void initView(View view, KnowledgeEntity.ChildListBean item, int position) {
        TextView tvTitle = view.findViewById(R.id.tvTitle);
        tvTitle.setText(item.getName());
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (type == 0) {
                    //知识库
                    Intent intent = new Intent(context, KnowledgeBaseSecondActivity.class);
                    intent.putExtra("parentCode", item.getCode());
                    intent.putExtra("folderNum", item.getFolderNum());
                    intent.putExtra("levelOne", levelOne);
                    if (item.getParentCode().equals("0")) {
//                        （0：全局 1: 题库 2:视频 3:资料库 4：图库 5：图文）
                        if ("资料库".equals(item.getName()) || "文件".equals(item.getName())) {
                            intent.putExtra("catalogCustomType", "1");
                            intent.putExtra("catalogType", "3");
                        } else if ("视频".equals(item.getName())) {
                            intent.putExtra("catalogCustomType", "2");
                            intent.putExtra("catalogType", "2");
                        } else if ("小视频".equals(item.getName())) {
                            intent.putExtra("catalogCustomType", "3");
                            intent.putExtra("catalogType", "2");
                        } else if ("图文".equals(item.getName())) {
                            intent.putExtra("catalogCustomType", "5");
                            intent.putExtra("catalogType", "5");
                        } else if ("图库".equals(item.getName())) {
                            intent.putExtra("catalogCustomType", "6");
                            intent.putExtra("catalogType", "4");
                        }
                    }
                    intent.putExtra("level", 2);
                    ArrayList<String> nameList = new ArrayList<>();//组织排列
                    nameList.add(levelOne.getName());
                    nameList.add(item.getName());
                    intent.putStringArrayListExtra("nameList", nameList);
                    context.startActivity(intent);
                } else if (type == 1) {
                    //视频
                    boolean oldDepartmentStatus = false;
                    try {
                        for (int j = 0; j < MyApp.videoFolderList.size(); j++) {
                            if (item.getCode().equals(MyApp.videoFolderList.get(j).getCode())) {
                                if (MyApp.videoFolderList.get(j).isSelect() || MyApp.videoFolderList.get(j).isHalfSelect()) {
                                    if (MyApp.videoFolderList.get(j).isSelect()) {
                                        oldDepartmentStatus = true;
                                    }
                                    MyApp.videoFolderList.remove(j);
                                    if (j < MyApp.videoFolderList.size()) {
                                        j--;
                                    }
                                }

                            }
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    Intent intent2=null;
                    if(videoType.equals("3")||videoType.equals("4")||videoType.equals("7")){
                        intent2 = new Intent(context, VideoNewCatesSecondActivity2.class);
                    }else{
                        intent2 = new Intent(context, VideoNewCatesSecondActivity.class);
                        intent2.putExtra("hierarchyOrgCode", item.getParentCode() + "_" + item.getCode());
                    }
                    intent2.putExtra("parentCode", item.getCode());
                    intent2.putExtra("parentName", item.getName());
                    intent2.putExtra("parentFileNum", item.getFileNum());
                    intent2.putExtra("videoType", videoType);
                    intent2.putExtra("levelOne", levelOne);
                    intent2.putExtra("level", 2);
                    KnowledgeEntity knowledgeEntity = new KnowledgeEntity();
                    knowledgeEntity.setUpperHalfSelect(item.isUpperHalfSelect());
                    knowledgeEntity.setSelect(item.isSelect());
                    knowledgeEntity.setHalfSelect(item.isHalfSelect());
                    knowledgeEntity.setHierarchyOrgCode(item.getParentCode());
                    knowledgeEntity.setCode(item.getCode());
                    knowledgeEntity.setFileNum(item.getFileNum());
                    knowledgeEntity.setFolderNum(item.getFolderNum());
                    knowledgeEntity.setTotalNum(item.getTotalNum());
                    knowledgeEntity.setName(item.getName());
                    knowledgeEntity.setParentCode(item.getParentCode());
                    intent2.putExtra("oldDepartmentEntity", knowledgeEntity);
                    ArrayList<String> nameList2 = new ArrayList<>();//组织排列
                    nameList2.add(levelOne.getName());
                    nameList2.add(item.getName());
                    intent2.putStringArrayListExtra("nameList", nameList2);
                    if (oldDepartmentStatus) {
                        intent2.putExtra("oldDepartmentStatus", true);
                    } else {
                        intent2.putExtra("oldDepartmentStatus", false);
                    }
                    context.startActivity(intent2);
                    MyApp.videoFolderMap.clear();
                } else if (type == 2) {
                    Intent intent = new Intent(context, DocumentBaseDirectorySecondActivity.class);
                    intent.putExtra("parentCode", item.getCode());
                    intent.putExtra("folderNum", item.getFolderNum());
                    intent.putExtra("catalogType", "3");
                    intent.putExtra("level", 2);
                    intent.putExtra("levelOne", levelOne);
                    ArrayList<String> nameList = new ArrayList<>();//组织排列
                    nameList.add(levelOne.getName());
                    nameList.add(item.getName());
                    intent.putStringArrayListExtra("nameList", nameList);
                    context.startActivity(intent);
                }

            }
        });
    }
}